<?php
declare(strict_types=1);

namespace App\Common\Extend\Guard;

use App\Common\Extend\Guard\Drives\SessionDrives;
use JetBrains\PhpStorm\ExpectedValues;

class Config
{
    const SECRET_SECRET = 'header';
    const SECRET_QUERY = 'query';
    const SECRET_SESSION = 'session';
    const SECRET_POST = 'post';


    private string $key;

    private string $drive;

    private int $ttl = 3600 * 24 * 30;

    private string $secret = 'header';

    private string $secret_key = 'header';

    private $guard;

    public function __construct(
        string                                                           $key,
        string                                                           $drive,
        int                                                              $ttl,
        #[ExpectedValues(['header', 'query', 'post', 'session'])] string $secret,
        string                                                           $secret_key,
        callable                                                         $guard
    )
    {
        $this->key = $key;
        $this->guard = $guard;
        if ($secret === 'session' || $drive === SessionDrives::class) {//选择sessio时驱动必须为session类型
            $this->secret = 'session';
            $this->drive = SessionDrives::class;
        } else {
            $this->secret = $secret;
            $this->drive = $drive;
        }
        $this->ttl = $ttl;
        $this->secret_key = $secret_key;
    }


    /**
     * 获取应用标识
     * @return string
     */
    public function getKey(): string
    {
        return $this->key;
    }

    /**
     * 获取储存驱动
     * @return string
     */
    public function getDrive(): string
    {
        return $this->drive;
    }


    /**
     * 获取过期时间
     * @return int
     */
    public function getTtl(): int
    {
        return $this->ttl ?: 3600 * 24 * 30;
    }


    /**
     * 获取凭证来源
     * @return string
     */
    public function getSecret(): string
    {
        return $this->secret;
    }


    /**
     * 获取凭据KEY
     * @return string
     */
    public function getSecretKey(): string
    {
        return $this->secret_key;
    }


    /**
     * 获取会员实例
     * @param int $key
     * @return ?ModelInterface
     */
    public function getGuard(int $key): ?ModelInterface
    {
        $function = $this->guard;
        return $function($key);
    }
}